//
//  ViewController.m
//  guaguake
//
//  Created by 石虎 on 2017/9/17.
//  Copyright © 2017年 石虎. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *converA;
@property (weak, nonatomic) IBOutlet UIImageView *converB;
/**
 *  是否可以刮奖
 */
@property(nonatomic,assign)BOOL enable;

@end

@implementation ViewController
// 手指触摸
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    if (touch.view == self.converB) {
        self.enable = YES;
    }
}
// 手指离开
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.enable = NO;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 1.获取到手指
    UITouch *touch = [touches anyObject];
    // 2.取出手指点击的位置
    CGPoint currentPoint = [touch locationInView:touch.view];
    
    if (self.enable) {
        // 刮开图层
        // 1.创建一个图形上下文
        UIGraphicsBeginImageContext(self.converB.frame.size);
        // 2.画图
        [self.converB.image drawInRect:self.converB.bounds];
        // 3.设置透明区域
        CGRect clearRect = CGRectMake(currentPoint.x - 10, currentPoint.y - 10, 20, 20);
        CGContextClearRect(UIGraphicsGetCurrentContext(), clearRect);
        // 4.获取绘制好的图片
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        // 5.设置绘制号的图片
        self.converB.image = newImage;
    }
    
}
@end
